package com.jd.order.service.impl;

import com.jd.order.domain.OrderPetAcquisition;
import com.jd.order.service.IOrderPetAcquisitionService;
import com.jd.basic.service.impl.BaseServiceImpl;
import com.jd.pet.domain.Pet;
import com.jd.pet.domain.SearchMasterMsg;
import com.jd.pet.dto.ConfirmDto;
import com.jd.pet.mapper.PetMapper;
import com.jd.pet.mapper.SearchMasterMsgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.nio.cs.ext.MS874;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jd
 * @since 2023-03-17
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private SearchMasterMsgMapper mapper;
    /**
     * 取消订单
     * @author kabuda
     * @date 2023/3/17 23:07

    */

    @Override
    @Transactional
    public void cancel(Long id) {
        // 修改收购单为取消
        OrderPetAcquisition order = super.findById(id);
        if (null != order) {
            order.setState(-1);
            super.update(order);
        }
        // 删除宠物信息
        petMapper.delete(order.getPetId());

        // 修改寻主信息状态，删除店铺id；
        SearchMasterMsg msg = new SearchMasterMsg();
        msg.setId(order.getMsgId());
        msg.setShopId(null);
        msg.setState(1);
        mapper.updateByOrderId(msg);
    }

    @Override
    public void confirm(ConfirmDto dto) {
        // 1 修改收购单状态为已完成
        OrderPetAcquisition order = super.findById(dto.getId());
        order.setState(1);
        order.setPrice(dto.getMoney());
        order.setPayType(dto.getPayType());
        super.update(order);
        // 2 修改寻主消息为已完成
        SearchMasterMsg msg = new SearchMasterMsg();
        msg.setId(order.getMsgId());
        msg.setState(4);
        mapper.update(msg);
        //  修改宠物信息
        Pet pet = new Pet();
        pet.setId(order.getPetId());
        pet.setCostprice(dto.getMoney());
        petMapper.update(pet);
    }
}
